package com.hp.controller;

import com.hp.context.Config;
import com.hp.dao.LinkDao;
import com.hp.entity.Link;
import com.hp.entity.PolularScienceResources;
import com.hp.entity.ResourcesType;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.PolularScienceResourcesService;
import com.hp.service.ResourcesTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

    @Controller
    @RequestMapping("/psr")
    public class PolularScienceResourcesController {
        @Autowired
        private PolularScienceResourcesService polularScienceResourcesService;
        @Autowired
        private LinkDao PolularScienceResourcesService;

        @Autowired
        private ResourcesTypeService resourcesTypeService;

        /**
         * 去添加资源页
         *
         * @return
         * @throws Exception
         */
        @RequestMapping("/toaddresource")
        public String toaddresource(Map<String, Object> map) throws Exception {
         //List<Link> list = PolularScienceResourcesService.findAll();
         //
            // map.put("typelist", liggst);
            List<ResourcesType> list =resourcesTypeService.getResourcesTypes(); //从数据库里查出的所有ResourcesType数据，用于显示类型列表
            map.put("typelist", list);
            return "resourcestype/addresource";
       }

        /**
         * 添加资源
         *
         * @param myfile
         * @param polularScienceResources
         * @param request
         * @return
         * @throws Exception
         */
        @ResponseBody
        @RequestMapping(value = "addresourcesubmit", method = RequestMethod.POST)
        public SubmitResultInfo addresourcesubmit(
                @RequestParam(value = "myfile", required = false) CommonsMultipartFile myfile,
                PolularScienceResources polularScienceResources,
                HttpServletRequest request) throws Exception {


            return polularScienceResourcesService.saveresource(myfile,
                    polularScienceResources, request);
        }

        /**
         * 去资源审核页面
         *
         * @return
         */
        @GetMapping(value = "/queryresoures")
        public String queryresoures() throws Exception{

            return "resourcestype/queryresoures";
    }


        /**
         * 查看所有没有审核的资源
         *
         * @param page
         * @param rows
         * @param polularScienceResources
         * @return
         * @throws Exception
         */
        @ResponseBody
        @RequestMapping("getAllAndIsN")
        public DataGridResultInfo getAllAndIsN(
                @RequestParam(name = "page", defaultValue = "1") int page,
                @RequestParam(name = "rows", defaultValue = "10") int rows,
                PolularScienceResources polularScienceResources) throws Exception {

            return polularScienceResourcesService.getAllAndIsN(page, rows,
                    polularScienceResources);
        }


        /**
         * 去修改资源页面
         * @return
         * @throws Exception
         */

        @RequestMapping("/toupdate")
        public String updateresource(String id,Map<String,Object> map) throws Exception {
            map.put("typelist",resourcesTypeService.pageresourcetype(1,Integer.MAX_VALUE).getRows());

            map.put("psr",polularScienceResourcesService.findByid(id));
           return "resourcestype/updateresource";
        }
        /**
         * 审核修改
         *
         * @param myfile
         * @param polularScienceResources
         * @param request
         * @return
         * @throws Exception
         */
        @ResponseBody
        @PostMapping("/upadtesubmit")
        public SubmitResultInfo upadtesubmit(@RequestParam(value = "myfile", required = false)CommonsMultipartFile myfile, PolularScienceResources polularScienceResources, HttpServletRequest request) throws Exception {
            return polularScienceResourcesService.updateresource(myfile,polularScienceResources,request);
        }



        /**
         * 审核列表删除
         * @param id
         * @return
         */
        @ResponseBody
        @PostMapping(value = "deleteIsNpsr")
        public SubmitResultInfo deleteIsNpsr(int id) throws Exception {
            return polularScienceResourcesService.deleteIsNpsr(id);
        }


        /**
         * 状态显示
         * @param id
         * @return
         * @throws Exception
         */
        @ResponseBody
        @PostMapping(value = "adoptpsr")
        public SubmitResultInfo adoptpsr(String id) throws Exception {
            String[] str = id.split(",");
            PolularScienceResources polularScienceResources = new PolularScienceResources();
            polularScienceResources.setId(Integer.parseInt(str[0]));
            polularScienceResources.setStatus(Integer.parseInt(str[1]));
            return polularScienceResourcesService.adoptpsr(polularScienceResources);
        }

        @ResponseBody
        @RequestMapping(value = "getByIdResource")
        public ResultInfo getByIdResource(int id)throws Exception{
            ResultInfo res=new ResultInfo();
            res.setMessageCode(200);
            res.getSysdata().put("psr",polularScienceResourcesService.xiugai(id));
            return res;

        }
    }


